{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[22]:\n",
    "\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#########  百度春联库使用说明  #################\n",
    "# baidu_couplets_create：初始化方法和获取对联方法\n",
    "# init初始化方法：\n",
    "#        需要传入百度AI应用提供的APP_ID、API_KEY和SECRET_KEY\n",
    "# get_couplets：获取对联方法方法\n",
    "#        request_url:百度api的链接\n",
    "#        word:对联命题关键字，以双引号括起来\n",
    "#################################################\n",
    "\n",
    "#########  调用方式举例  #################\n",
    "# #导入标准库\n",
    "# import sys\n",
    "# import os\n",
    "\n",
    "# #传入参数\n",
    "# baidu_app_id = '20217336'\n",
    "# baidu_api_key = 'cGIn10y839IumZjhUSu0CQ9W'\n",
    "# baidu_api_secret = 'FaZDHtAGYFurNtXZoPHso7PbjL7vTsUP'\n",
    "\n",
    "# baidu_request_url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets'\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "#     try:\n",
    "#         couplets = baidu_couplets_create(baidu_app_id, baidu_api_key, baidu_api_secret)\n",
    "        \n",
    "#         word = couplets.get_couplets(baidu_request_url, \"春天\")\n",
    "        \n",
    "#         print(word.json())\n",
    "                       \n",
    "#     except KeyboardInterrupt:             \n",
    "#         # 释放cap,销毁窗口\n",
    "#         capture.release()                                   \n",
    "#         cv.destroyAllWindows()                                  \n",
    "#         print(\"任务被终止\")\n",
    "#################################################        \n",
    "\n",
    "from aip import AipSpeech\n",
    "import requests\n",
    "import base64\n",
    "import re\n",
    "import json\n",
    "\n",
    "class baidu_couplets_create(object):\n",
    "    \n",
    "    #百度AI应用提供参数\n",
    "    APP_ID      = None\n",
    "    API_KEY     = None\n",
    "    SECRET_KEY  = None\n",
    "    access_token      = None\n",
    "    \n",
    "    def __init__(self, app_id, api_key, secret_key):\n",
    "        #获取提供的百度AI接口参数\n",
    "        self.APP_ID = app_id\n",
    "        self.API_KEY = api_key\n",
    "        self.SECRET_KEY = secret_key\n",
    "        \n",
    "        #获取access_token值\n",
    "        main_host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='\n",
    "        host = main_host + self.API_KEY + '&client_secret=' + self.SECRET_KEY\n",
    "        response = requests.get(host)\n",
    "        self.access_token = response.json()['access_token']\n",
    "        \n",
    "    def get_couplets(self, request_url, word):\n",
    "        #对要文字进行格式转化\n",
    "        params = dict()\n",
    "        params['index'] = 0\n",
    "        params['text'] = word\n",
    "        params = json.dumps(params).encode('utf-8')\n",
    "        \n",
    "        #填充请求对联格式，进行获取对联\n",
    "        request_url = request_url + \"?access_token=\" + self.access_token\n",
    "        headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "        response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "        #返回获取到的春联\n",
    "        return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
